package com.feature.learn.thread.base;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Copyright (C), 2015-2020, 顺丰科技有限公司
 *
 * @author 01390942
 * @Description
 * @create 2020/2/14
 * @since 1.0.0
 */
public class ThreadRunInOrder {

    static ExecutorService executorService = Executors.newFixedThreadPool(1);

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        otherThread();
        System.out.println("invoke end");
        executorService.shutdown();
    }

    private static void otherThread() throws ExecutionException, InterruptedException {
        mannThread();
        exeThread();
//        submitThread();
    }

    private static void mannThread() throws InterruptedException {
        System.out.println("main thread");
        Thread.sleep(2000);
    }

    private static void submitThread() throws ExecutionException, InterruptedException {
        Future<?> subThread = executorService.submit(() -> {
            System.out.println("sub thread");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        subThread.get();
    }

    private static void exeThread() {
       executorService.execute(() -> {
            System.out.println("sub thread");
            System.out.println("===================");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }


}